﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_Lines : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getBuses buses = new getBuses();
            ctlLines.DataSource = buses.Read();
            ctlLines.DataBind();
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key", "crtaj();", true);
        }
    }
    protected void ctlLines_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int _roundTime = 0;
        int _busNo = 0;
        string _startStation;
        string _endStation;
        getBusesByBusNo _bus = null;

        string _scriptBuild = string.Empty;
        string commandArg = e.CommandArgument.ToString();
        _busNo = Int32.Parse(commandArg.Trim());
        _bus = new getBusesByBusNo(_busNo);
        
        _roundTime = (int)_bus.Read().Rows[0]["RoundTime"];
        getStationForStationId _startParam = new getStationForStationId((int)_bus.Read().Rows[0]["StartStation"]);
        getStationForStationId _endParam = new getStationForStationId((int)_bus.Read().Rows[0]["EndStation"]);
        _startStation = _startParam.Read().Rows[0]["Description"].ToString();
        _endStation = _endParam.Read().Rows[0]["Description"].ToString();
        txtBusNumber.Text = _busNo.ToString();
        txtRoundTime.Text = _roundTime.ToString();
        txtStartStation.Text = _startStation;
        txtEndStation.Text = _endStation;

        int _busID = (int)_bus.Read().Rows[0]["BusID"];
        getLatLngForStation _latlngStation = new getLatLngForStation(_busID);
        DataTable _latlngTable = _latlngStation.Read();
        float _sLatitude;
        float _sLongitude;
        string _sDescription;
        int _sOrder;

        _scriptBuild += "var _sWindow;";
        for (int i = 0; i < _latlngTable.Rows.Count; i++)
        {
            //todo
            _sLatitude = (float)_latlngTable.Rows[i]["SLatitude"];
            _sLongitude = (float)_latlngTable.Rows[i]["SLongitude"];
            _sOrder = (int)_latlngTable.Rows[i]["PassingOrder"];
            _sDescription = _latlngTable.Rows[i]["Description"].ToString();
            string _content = "Број на станица:  " + _sOrder + "<br />" + "Име на станица:  " + _sDescription + "<br />";
            _scriptBuild += "var _mImage = new google.maps.MarkerImage('../images/busstation.png',new google.maps.Size(24, 24),new google.maps.Point(0,0),new google.maps.Point(15, 15));";
            _scriptBuild += "var _sMarker = new google.maps.Marker({ position: new google.maps.LatLng(" + _sLatitude + "," + _sLongitude + ")});";
            _scriptBuild += "_sMarker.setIcon(_mImage);";
            _scriptBuild += "_sMarker.setMap(map);";
            _scriptBuild += "google.maps.event.addListener(_sMarker, 'click', function(e) {";
            _scriptBuild += "if(_sWindow==null)";
            _scriptBuild += "_sWindow = new google.maps.InfoWindow();";
            _scriptBuild += "_sWindow.setContent('" + _content + "'); _sWindow.setPosition(e.latLng); _sWindow.open(map);";
            _scriptBuild += " });";
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key", "crtaj();" + _scriptBuild, true);
    }
}